<?php

App::uses('AppController', 'Controller');

/**
 * Controller
 *
 * @property 
 */
class LineaslaboratoriosController extends AppController {

    public $layout = 'hospital';
    public $uses = array('Lineaslaboratorio','Medicamento');
     /**
     * index method
     *
     * @return void
      * 
     */
    public function beforeFilter() {
        parent::beforeFilter();
        //$this->Auth->allow();
    }
    public function index() {
        $lineasl = $this->Lineaslaboratorio->find('all', array('recursive'=>-1));
        $this->set(compact('lineasl'));
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Lineaslaboratorio->exists($id)) {
            throw new NotFoundException(__('Invalid lineaslaboratorio'));
        }
        $options = array('conditions' => array('Lineaslaboratorio.' . $this->Lineaslaboratorio->primaryKey => $id));
        $this->set('Lineaslaboratorio', $this->Lineaslaboratorio->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        
        if ($this->request->is('post')) {
            //debug($this->request->data); exit;
            $this->Lineaslaboratorio->create();            
            if ($this->Lineaslaboratorio->save($this->request->data)) {
                $this->Session->setFlash(__('The lineaslaboratorio has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The lineaslaboratorio could not be saved. Please, try again.'));
            }
        }       
        else{
            $medicamentos = $this->Medicamentos->find('all',
                    array('fields'=>array('Medicamentos.id','Medicamentos.nombre'),
                        'recursive'=>-1
                        ));            
            $this->set(compact('medicamentos'));   
        } 
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->Lineaslaboratorio->id = $id;
        if (!$this->Lineaslaboratorio->exists($id)) {
            throw new NotFoundException(__('Invalid lineaslaboratorio'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            //debug($this->request->data);exit;
            
            if ($this->Lineaslaboratorio->save($this->request->data)) {
                $this->Session->setFlash(__('The lineaslaboratorio has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The lineaslaboratorio could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Lineaslaboratorio.' . $this->Lineaslaboratorio->primaryKey => $id));
            $this->request->data = $this->Lineaslaboratorio->find('first', $options);
        }        
        //we have to use another relationshif table
        $medicamentos=$this->Medicamentos->find('all',array('recursive'=>-1));
        //$parametros = $this->Parametro->find('list');
        $this->set(compact('medicamentos'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    
    public function delete($id = null) {
        $this->Lineaslaboratorio->id = $id;
        
        if (!$this->Lineaslaboratorio->exists()) {
            throw new NotFoundException(__('Invalid lineaslaboratorio'));
        }
       // $this->request->onlyAllow('post', 'delete');
        
        if ($this->Lineaslaboratorio->delete()) {
            $this->Session->setFlash(__('lineaslaboratorio deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('lineaslaboratorio was not deleted'));
        $this->redirect(array('action' => 'index'));
    }    
}
